#include <iostream>
#include "key_tree_view.h"
#include "main_form.h"


Key_Tree_View::Key_Tree_View()
{
	Gtk::Menu * new_item_sub_menu = Gtk::manage(new Gtk::Menu());;
	Gtk::Menu::MenuList & sub_list = new_item_sub_menu->items();
	sub_list.push_back( Gtk::Menu_Helpers::MenuElem("项(_K)",
		sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	sub_list.push_back(Gtk::Menu_Helpers::SeparatorElem::SeparatorElem());
	sub_list.push_back( Gtk::Menu_Helpers::MenuElem("字符串值(_S)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	sub_list.push_back( Gtk::Menu_Helpers::MenuElem("二进制值(_B)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	sub_list.push_back( Gtk::Menu_Helpers::MenuElem("DWORD值(_D)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	sub_list.push_back( Gtk::Menu_Helpers::MenuElem("多字符串值(_M)",
			sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	sub_list.push_back( Gtk::Menu_Helpers::MenuElem("可扩充字符串值(_E)",
		sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	new_item_sub_menu->accelerate(*this);

	Gtk::Menu::MenuList & menulist = popup_menu_.items();
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("展开/折叠",
		sigc::mem_fun(*this, &Key_Tree_View::on_expanded) ) );
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("新建(_N)", *new_item_sub_menu));
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("查找(_F)",
		sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("删除(_D)",
		sigc::mem_fun(*this, &Key_Tree_View::on_delete) ) );
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("重命名(_R)",
		sigc::mem_fun(*this, &Key_Tree_View::on_create_key) ) );
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("导出(_E)",
		sigc::mem_fun(*this, &Key_Tree_View::on_key_path_exported) ) );
	menulist.push_back( Gtk::Menu_Helpers::MenuElem("复制项名称(_C)",
		sigc::mem_fun(*this, &Key_Tree_View::on_key_path_copy) ) );

	popup_menu_.accelerate(*this);
	signal_row_activated().connect(sigc::mem_fun(*this,
			&Key_Tree_View::on_tree_row_activated));
}

Key_Tree_View::~Key_Tree_View()
{
}

bool
Key_Tree_View::on_button_release_event(GdkEventButton* event)
{
	Gtk::TreeView::on_button_release_event(event);

  if( (event->type == GDK_BUTTON_RELEASE) && (event->button == 3) )
  {
   Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
   {
   	 Gtk::TreeModel::iterator iter = refSelection->get_selected();
    	if(iter) {
    		// 根据实际情况动态更新弹出菜单中的“展开”及“折叠”选项
			Gtk::TreeModel::Path path(iter);
			Gtk::Menu::MenuList & menulist = popup_menu_.items();
			if (path.size() == 0) {
				//TODO: 如果没有子结点，可将展开项disable掉
			}
			menulist.erase(menulist.begin());
			if (row_expanded(path)) { // 键已经展开，将折叠
				menulist.push_front( Gtk::Menu_Helpers::MenuElem("折叠",
					sigc::mem_fun(*this, &Key_Tree_View::on_expanded) ) );
			} else {
				menulist.push_front( Gtk::Menu_Helpers::MenuElem("展开",
					sigc::mem_fun(*this, &Key_Tree_View::on_expanded) ) );
			}
      	popup_menu_.popup(event->button, event->time);
		}
	}

    return true; //It has been handled.
  }
  else
    return false;
}

void
Key_Tree_View::on_create_key()
{
  std::cout << "A sub menu item was selected." << std::endl;
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			create_key_(iter);
		}
	}
}

void
Key_Tree_View::on_expanded()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter) {
			Gtk::TreeModel::Path path(iter);
			if (row_expanded(path)) { // 键已经展开，将折叠
				collapse_row(path);
			} else {
				expand_to_path(path);
			}
		}
	}
}

/*void	Key_Tree_View::on_modify()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			Gtk::TreeModel::Path path(iter);
			edit_selected_(path, get_column(*path.begin()));
		}
	}
}*/

void
Key_Tree_View::on_delete()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			delete_key_selected_(iter);
		}
	}
}

void Key_Tree_View::on_key_path_copy()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			key_whole_path_copy_(iter);
		}
	}
}

void Key_Tree_View::on_key_path_exported()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			key_path_exported_(iter);
		}
	}
}

void Key_Tree_View::on_tree_row_activated(const Gtk::TreeModel::Path & path,
		Gtk::TreeViewColumn *column)
{
	if (row_expanded(path)) { // 键已经展开，将被折叠
		collapse_row(path);
	} else {
		expand_to_path(path);
	}
}



/*
void
Key_Tree_View::on_rename()
{
}*/
